/**
 * 游戏列表菜单的管理
 */
(function() {
    var gameList = function() {
        var self = this;
    };
    gameList.prototype = {};

    gameList.prototype.init = function(cb) {
        var self = this;

        // 本地的配置
        self.local = Util.getMenuGames();
        
        // 服务器的配置
        self.pull(function() {
            self.draw();
            if (cb) cb();
        });
    }

    gameList.prototype.pull = function(cb) {
        var self = this;
        self.menus = [];
        if (window.localStorage) {
            // 先从本地读取
            var v1 = localStorage.getItem(urlInfo.host + "/remote_games");
            var v2 = localStorage.getItem(urlInfo.host + "/remote_games_time");
            if (v1 && v2 && new Date().getTime() - v2 < 120 * 60 * 100) {
                // 有效期为2分钟
                self.menus = JSON.parse(v1);
            }
        }

        if (Util.isEmpty(self.menus)) {
            Classify.queryExt("games", "order", true, {}, function(r) {
                for (var i in r) {
                    if (r[i] < 0) continue;
                    self.menus.push({
                        game: i,
                        order: r[i],
                    });
                    self.menus.sort(function(a, b) {
                        var aorder = a.order, border = b.order;
                        return aorder - border;
                    });
                }
                cb();
            });
        }
        else cb();
    }

    gameList.prototype.parseMenus = function(tops, others) {
        var self = this;

        if (self.local.length == 0) {
            // 没有没有做配置
            for (var i = 0; i < 9; i++) {
                if (i >= self.menus.length) break;
                tops.push(self.menus[i]);
            }
            for (var i = 9; i < self.menus.length; i++) {
                others.push(self.menus[i]);
            }
        }
        else {
            for (var i in self.local) {
                for (var j in self.menus) {
                    if (self.menus[j].game == self.local[i]) {
                        tops.push(self.menus[j]);
                        break;
                    }
                }
            }

            for (var i in self.menus) {
                if (self.local.indexOf(self.menus[i].game) == -1) {
                    others.push(self.menus[i]);
                }
            }
        }
    }

    gameList.prototype.draw = function() {
        var self = this;
        var curr = window.game || urlInfo.params.game || 'bjpk10';
        if (self.menus[curr] < 0) {
            // 这个游戏关闭了，需要使用另外一个
            for (var k in self.menus) {
                if (self.menus[k] > 0) {
                    curr = k;
                    break;
                }
            }
        }

        // 设置为当前的游戏
        window.game = curr;

        // 填充菜单
        var tops = [], others = [];
        self.parseMenus(tops, others);
        $("#homeMenus").empty();
        var l = '<a href="#" game="__game__" class="__c__"><span>__name__</span><i class="editbtn"></i></a>';
        for (var i in tops) {
            tops[i].top = true;
            var a = l.replace(/__name__/, get_game_display_name(tops[i].game));
            if (tops[i].game == curr)
                a = a.replace(/__c__/, "selected");
            else
                a = a.replace(/__c__/, "");
            a = a.replace(/__game__/, tops[i].game);
            $("#homeMenus").append(a);
        }
    
        // 更多游戏
        $("#moreGameBtn a:first").removeClass("selected");
        $("#moreGameBtn .currentGameTxt").html("更多游戏");
        $("#gamebox").empty();
        var l = '<a href="#" game="__game__"><span>__name__</span><i class="editbtn"></i></a>';
        for (var i in others) {
            others[i].top = false;
            var a = l.replace(/__name__/, get_game_display_name(others[i].game));
            if (others[i].game == curr) {
                $("#moreGameBtn a:first").addClass("selected");
                $("#moreGameBtn .currentGameTxt").html(get_game_display_name(others[i].game));
            }
                
            a = a.replace(/__c__/, "");
            a = a.replace(/__game__/, others[i].game);
            $("#gamebox").append(a);
        }
    
        // 初始化菜单编辑
        MoveMenu.init(tops);
    }

    window.GameList = new gameList();
})();
